<?php
/*
 * Aclass_user 用户
 * 2012-05-08
 * abans@qq.com
 */
class Com_user{
	function  __construct($appid='') {
		$this->cname = 'Acan:user:';
		$this->_SS = Afun_session();
		$this->model = Afun_db()->model(Acan_table_user);
	}
	//获取用户kv
	function lists($type='kv'){
		if($type=='kv') $con->select = 'a.uid,a.username';
		$arr = $this->model->findAll($con);
		if($type=='kv')
			foreach ($arr as $k=>$v) { 
				$data[$v['uid']] = $v['username'];
			}
		return $data;
	}
	//获取单条用户记录
	function find($con){
		$data = $this->model->ofind($con);
		if($data)
			unset($data->password,$data->passwordx);//返回数据时删除密码信息
		return $data;
	}
	//获取单条用户记录
	function finduid($uid){
		$data = $this->model->ofindPk($uid);
		if($data)
			unset($data->password,$data->passwordx);//返回数据时删除密码信息
		return $data;
	}
	//检测用户记录存在
	function exist($con){
		$data = $this->find($con);
		if($data){
			return true;
		}else{
			return false;
		}
	}
	//获取用户信息
	function info($uid){
		$info = $this->model->ofindPk($uid);
		unset($info->password,$info->passwordx);
		return $info;
	}
	/*
	添加用户
	是否启用随机生成密码
	$random : 0,1
	是否空用户名
	$urand : 0 1
	*/
	function add($save,$random=0,$urand=0){
		if(!is_array($save) && !is_object($save) ){return false;}
		$error = true;
		if(is_object($save)){
			$save = Afun_obj_arr($save);
		}
		if($error && empty($save['username']) && $urand==0){$error=false;$msg="用户名不能为空";}
		if($error && $save['username']){
			if($this->exist(array('username'=>$save['username']))){
				$error=false;$msg="用户名已经存在";
			}
		}
		if($error && $save['mail']){
			if($this->exist(array('mail'=>$save['mail']))){
				$error=false;$msg="邮箱已经存在";
			}
		}
		//密码为空不保存
		if($error && $random==0 && empty($save['password'])){
			unset($save['password']);$error=false;$msg="密码不能为空";
		}
		//产生8位随机密码
		if($error && $random==1){
			$save['password'] = substr(md5(rand(1,999).$save['username']),5,8);
		}
		return $this->save($save,$error,$msg);
	}
	/*
	更新用户
	*/
	function save($save,$error=true,$msg=''){
		if(is_object($save)) $save=Afun_obj_arr($save);
		if($save['info']){
			$info = $save['info'];unset($save['info']);
		}
		if(count($save)>1){
			//添加新用户 记录ip和注册时间
			if($error && empty($save['uid'])){
				if(empty($save['regip'])){$save['regip'] = Afun_get_ip();}
				$save['regdate'] = Afun_get_time();
			}
			$save['regip'] = Afun_get_ip();
			$save['regdate'] = Afun_get_time();
			if($error){
				if($save['password']){
					if(isset($save['password']) && empty($save['password'])){$save['password']='123456';}
					$save['passwordx'] = $save['password'];
					$save['password'] = Afun_pass($save['password']);
				}
				$status = $this->model->save($save);
				if(empty($save['uid'])){
					$save['uid'] = $this->model->lastInsertId();
					$msg="成功添加用户";
					if($save['password']){$save['password'] = $save['passwordx'];}
				}else{
					$msg="成功更新用户";
				}
				if($save['password']){unset($save['passwordx']);}
			}
		}
		if($info){
			$info['uid'] = $save['uid'];
			$rsi = $this->save_info($info);
		}
		$rs->data = $save;
		$rs->status = $status;
		$rs->status_info = $rsi;
		$rs->error = $error;
		$rs->msg = $msg;
		$rs->uid = $save['uid'];
		return $rs;
	}
	//保存用户信息
	function save_info($save){
		$rsi = Afun_db()->model(Acan_table_user_info)->save($save,2);
		if($rsi>0){
			$this->load_info($save['uid']);
		}
		return $rsi;
	}
	//更新用户信息
	function load_info($uid){
		$ss = Afun_session();
		if($ss['uid']==$uid){
			$info = Afun_db()->model(Acan_table_user_info)->find('uid='.$ss['uid']);
			$ss['info']=$info;
			Afun_session($ss);
		}
	}
	//保存用户账户金额
	function save_cash($save){
		$rsi = Afun_db()->model(Acan_table_user_cash)->save($save,2);
		if($rsi>0){
			$this->load_cash($save['uid']);
		}
		return $rsi;
	}
	//更新用户账户金额
	function load_cash($uid){
		$ss = Afun_session();
		if($ss['uid']==$uid){
			$cash = Afun_db()->model(Acan_table_user_cash)->find('uid='.$ss['uid']);
			$ss['cash']=$cash;
			Afun_session($ss);
		}
	}
	
}
?>
